package common

import "strings"

// CardholderStatus 统一的持卡人状态常量
const (
	// 正常状态
	CardholderStatusNormal = "normal" // 可用

	// 失效状态
	CardholderStatusDisabled = "disabled" // 失效

	// 其他状态（保持向后兼容）
	CardholderStatusActive   = "active"   // 活跃（兼容旧状态）
	CardholderStatusInactive = "inactive" // 不活跃（兼容旧状态）
)

// CardholderStatusMapping 平台状态到统一状态的映射
type CardholderStatusMapping struct {
	PhotonPay map[string]string
	HlPay     map[string]string
}

// GetCardholderStatusMapping 获取持卡人状态映射
func GetCardholderStatusMapping() *CardholderStatusMapping {
	return &CardholderStatusMapping{
		PhotonPay: map[string]string{
			// PhotonPay 状态映射
			"NORMAL":   CardholderStatusNormal,
			"ACTIVE":   CardholderStatusNormal,
			"INACTIVE": CardholderStatusDisabled,
			"DISABLED": CardholderStatusDisabled,
		},
		HlPay: map[string]string{
			// HlPay 状态映射
			"ACTIVE":   CardholderStatusNormal,
			"NORMAL":   CardholderStatusNormal,
			"INACTIVE": CardholderStatusDisabled,
			"DISABLED": CardholderStatusDisabled,
		},
	}
}

// MapPlatformCardholderStatusToUnified 将平台持卡人状态映射为统一状态
func MapPlatformCardholderStatusToUnified(platform string, platformStatus string) string {
	mapping := GetCardholderStatusMapping()

	// 转换为大写以进行匹配
	upperStatus := strings.ToUpper(platformStatus)

	switch platform {
	case "photonpay":
		if unifiedStatus, exists := mapping.PhotonPay[upperStatus]; exists {
			return unifiedStatus
		}
	case "hlpay":
		if unifiedStatus, exists := mapping.HlPay[upperStatus]; exists {
			return unifiedStatus
		}
	}

	// 如果没有找到映射，返回默认状态
	return CardholderStatusNormal
}

// MapUnifiedCardholderStatusToPlatform 将统一持卡人状态映射为平台状态
func MapUnifiedCardholderStatusToPlatform(platform string, unifiedStatus string) string {
	mapping := GetCardholderStatusMapping()

	// 反向映射
	switch platform {
	case "photonpay":
		for platformStatus, unified := range mapping.PhotonPay {
			if unified == unifiedStatus {
				return platformStatus
			}
		}
	case "hlpay":
		for platformStatus, unified := range mapping.HlPay {
			if unified == unifiedStatus {
				return platformStatus
			}
		}
	}

	// 如果没有找到映射，返回默认状态
	switch platform {
	case "photonpay":
		return "NORMAL"
	case "hlpay":
		return "ACTIVE"
	default:
		return "NORMAL"
	}
}

// IsValidCardholderStatus 检查持卡人状态是否有效
func IsValidCardholderStatus(status string) bool {
	validStatuses := []string{
		CardholderStatusNormal,
		CardholderStatusDisabled,
		CardholderStatusActive,   // 兼容旧状态
		CardholderStatusInactive, // 兼容旧状态
	}

	for _, validStatus := range validStatuses {
		if status == validStatus {
			return true
		}
	}
	return false
}

// GetCardholderStatusDescription 获取持卡人状态描述
func GetCardholderStatusDescription(status string) string {
	descriptions := map[string]string{
		CardholderStatusNormal:   "可用",
		CardholderStatusDisabled: "失效",
		CardholderStatusActive:   "活跃",
		CardholderStatusInactive: "不活跃",
	}

	if description, exists := descriptions[status]; exists {
		return description
	}
	return "未知状态"
}

// IsCardholderActive 检查持卡人是否处于可用状态
func IsCardholderActive(status string) bool {
	activeStatuses := []string{
		CardholderStatusNormal,
		CardholderStatusActive,
	}

	for _, activeStatus := range activeStatuses {
		if status == activeStatus {
			return true
		}
	}
	return false
}

// IsCardholderDisabled 检查持卡人是否处于失效状态
func IsCardholderDisabled(status string) bool {
	disabledStatuses := []string{
		CardholderStatusDisabled,
		CardholderStatusInactive,
	}

	for _, disabledStatus := range disabledStatuses {
		if status == disabledStatus {
			return true
		}
	}
	return false
}

// NormalizeCardholderStatus 标准化持卡人状态（将旧状态转换为新状态）
func NormalizeCardholderStatus(status string) string {
	switch strings.ToLower(status) {
	case "active", "normal":
		return CardholderStatusNormal
	case "inactive", "disabled":
		return CardholderStatusDisabled
	default:
		return CardholderStatusNormal
	}
}
